package com.lianmai.chaincard.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lianmai.chaincard.MyApplication;
import com.lianmai.chaincard.R;
import com.lianmai.chaincard.adapter.MineTitleAdapter;
import com.lianmai.chaincard.bean.ImageTitleBean;
import com.lianmai.chaincard.widget.CustomCircleProgressBar;
import com.lianmai.chaincard.widget.GlideCircleTransform;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.youth.xframe.utils.imageload.XImage;
import com.youth.xframe.widget.XToast;

import org.bining.footstone.utils.FileUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 * 创建日期：2018/8/15 0015
 * 作者:Kite
 */
public class HomeMineFragment extends BaseFragment {

    @BindView(R.id.rv_mine)
    RecyclerView rvMineRecycler;//总布局

    private ImageView ivUserHead;//用户头像

    private TextView tvUpdate;//立即升级

    private CustomCircleProgressBar ivPercent;

    private MineTitleAdapter mAdapter;

    private Animation animation;

    private List<ImageTitleBean> mList = new ArrayList<>();

    private List<ImageTitleBean> mCardManagementList = new ArrayList<>();//名片管理

    private List<ImageTitleBean> mGeneralizeCardList = new ArrayList<>();//推广名片

    private List<ImageTitleBean> mServiceVipList = new ArrayList<>();//会员服务

    private List<ImageTitleBean> mShopManagementList = new ArrayList<>();//商店管理

    private int[] cardManagementImageList = new int[]{R.drawable.card_management_one, R.drawable.card_management_two, R.drawable.card_management_three,
            R.drawable.card_management_four, R.drawable.card_management_five, R.drawable.card_management_six, R.drawable.card_management_seven};

    private int[] generalizeCardImageList = new int[]{R.drawable.generalize_card_one, R.drawable.generalize_card_two, R.drawable.generalize_card_three,
            R.drawable.generalize_card_four, R.drawable.generalize_card_five, R.drawable.generalize_card_six, R.drawable.generalize_card_seven};

    private int[] serviceVipImageList = new int[]{R.drawable.service_vip_one, R.drawable.service_vip_two, R.drawable.service_vip_three, R.drawable.service_vip_four, R.drawable.service_vip_five};

    private int[] shopManagementImageList = new int[]{R.drawable.shop_management_one, R.drawable.shop_management_two, R.drawable.shop_management_three};

    /**
     * Fragment 实例
     */
    public static HomeMineFragment newInstance(Object... pramars) {
        HomeMineFragment fragment = new HomeMineFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_mine, container, false);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        initLocationData();
    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        ivPercent.setProgress(50);
        tvUpdate.startAnimation(animation);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST://如果裁剪，不要用原路径
                    List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                    XImage.getInstance().load(ivUserHead, localMedia.get(0).getCutPath(), new GlideCircleTransform(getActivity()));
                    break;
            }
        }
    }

    private void initLocationData() {
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.mine_head_layout, null);

        ivPercent = (CustomCircleProgressBar) headView.findViewById(R.id.iv_percent);
        ivPercent.setProgress(50);

        tvUpdate = (TextView) headView.findViewById(R.id.tv_update);
        animation = AnimationUtils.loadAnimation(getActivity(), R.anim.scale_animation);
        tvUpdate.startAnimation(animation);

        ivUserHead = (ImageView) headView.findViewById(R.id.iv_user_head);
        ivUserHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choosePicture();
            }
        });

        String[] cardManagementArray = getResources().getStringArray(R.array.card_management_list);
        String[] generalizeCardArray = getResources().getStringArray(R.array.generalize_card_list);
        String[] serviceVipArray = getResources().getStringArray(R.array.service_vip_list);
        String[] shopManagementArray = getResources().getStringArray(R.array.shop_management_list);

        for (int i = 0; i < cardManagementArray.length; i++) {
            mCardManagementList.add(new ImageTitleBean(cardManagementImageList[i], cardManagementArray[i]));
        }

        for (int i = 0; i < generalizeCardArray.length; i++) {
            mGeneralizeCardList.add(new ImageTitleBean(generalizeCardImageList[i], generalizeCardArray[i]));
        }

        for (int i = 0; i < serviceVipArray.length; i++) {
            mServiceVipList.add(new ImageTitleBean(serviceVipImageList[i], serviceVipArray[i]));
        }

        for (int i = 0; i < shopManagementArray.length; i++) {
            mShopManagementList.add(new ImageTitleBean(shopManagementImageList[i], shopManagementArray[i]));
        }

        mList.add(new ImageTitleBean(R.drawable.visiting_card_management, "名片管理", mCardManagementList));
        mList.add(new ImageTitleBean(R.drawable.generalize_visiting_card, "推广名片", mGeneralizeCardList));
        mList.add(new ImageTitleBean(R.drawable.service_vip, "会员服务", mServiceVipList));
        mList.add(new ImageTitleBean(R.drawable.shop_management, "商店管理", mShopManagementList));

        mAdapter = new MineTitleAdapter(rvMineRecycler, mList);
        mAdapter.getRecyclerView().setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.addHeaderView(headView);
        rvMineRecycler.setAdapter(mAdapter);

    }

    /**
     * 选择头像
     */
    private void choosePicture() {
        PictureSelector.create(HomeMineFragment.this)
                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                //.theme(R.style.style_picture_white)//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                .maxSelectNum(1)// 最大图片选择数量 int
                .minSelectNum(0)// 最小选择数量 int
                .imageSpanCount(4)// 每行显示个数 int
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .previewImage(true)// 是否可预览图片 true or false
                .previewVideo(false)// 是否可预览视频 true or false
                .enablePreviewAudio(false) // 是否可播放音频 true or false
                .isCamera(true)// 是否显示拍照按钮 true or false
                .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                .setOutputCameraPath(FileUtils.getAppDefPath(FileUtils.PATH_IMAGE))// 自定义拍照保存路径,可不填
                .enableCrop(true)// 是否裁剪 true or false
                .compress(true)// 是否压缩 true or false
                // .glideOverride()// int glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .withAspectRatio(1, 1)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                // .hideBottomControls()// 是否显示uCrop工具栏，默认不显示 true or false
                // .isGif()// 是否显示gif图片 true or false
                .compressSavePath(FileUtils.getAppDefPath(FileUtils.PATH_IMAGE))//压缩图片保存地址
                .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
                .circleDimmedLayer(false)// 是否圆形裁剪 true or false
                .showCropFrame(true)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                .showCropGrid(true)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                .openClickSound(false)// 是否开启点击声音 true or false
                // .selectionMedia()// 是否传入已选图片 List<LocalMedia> list
                // .previewEggs()// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                // .cropCompressQuality()// 裁剪压缩质量 默认90 int
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .synOrAsy(true)//同步true或异步false 压缩 默认同步
                // .cropWH()// 裁剪宽高比，设置如果大于图片本身宽高则无效 int
                .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                // .videoQuality()// 视频录制质量 0 or 1 int
                //  .videoMaxSecond(15)// 显示多少秒以内的视频or音频也可适用 int
                // .videoMinSecond(10)// 显示多少秒以内的视频or音频也可适用 int
                // .recordVideoSecond()//视频秒数录制 默认60s int
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

}
